package com.xgst.land.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.core.view.forEach
import androidx.fragment.app.Fragment
import com.azhon.appupdate.util.LogUtil.Companion.b
import com.blankj.utilcode.util.ToastUtils
import com.xgst.land.R
import com.xgst.land.activity.check.InventoryScanLandActivity
import com.xgst.land.databinding.FragmentScanQueryBinding
import com.xgst.land.databinding.FragmentScanResultBinding

//盘点 查询样本
class ScanQueryFragment:Fragment(), View.OnClickListener {

    private lateinit var binding: FragmentScanQueryBinding

    private lateinit var mActivity:InventoryScanLandActivity

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentScanQueryBinding.inflate(layoutInflater)
        init()
        return binding.root
    }

    fun init(){
        mActivity = activity as InventoryScanLandActivity
        binding.imgAdd.setOnClickListener(this)
        binding.tvQuery.setOnClickListener(this)
    }

    fun reset(){
        binding.llSearch.removeAllViews()
        binding.etName.setText("")
    }

    override fun onClick(view: View?) {
        when(view){
            binding.imgAdd -> {
                val childView = LayoutInflater.from(context).inflate(R.layout.fragment_scan_query_child_layout,null,false)
                binding.llSearch.addView(childView)
            }
            //定位
            binding.tvQuery -> {
                val nameList = arrayListOf<String>()
                val uidName = binding.etName.text.toString()
                if(uidName.isEmpty()){
                    ToastUtils.showShort("请输入样本名称")
                    return
                }
                nameList += uidName
                binding.llSearch.forEach {
                    val etName = it.findViewById<EditText>(R.id.etName)
                    val childUidName = etName.text.toString()
                    if(childUidName.isNotEmpty()){
                        nameList += childUidName
                    }
                }
                mActivity.locationUID(nameList)
            }
        }
    }
}